package cn.iocoder.yudao.module.bpm.controller.admin.tohandle.task;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.bpm.controller.admin.tohandle.task.vo.*;
import cn.iocoder.yudao.module.bpm.convert.tohandle.task.BpmHandleTaskConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.tohandle.BpmHandleTaskDO;
import cn.iocoder.yudao.module.bpm.service.tohandle.task.BpmHandleTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 审批流待办中心——任务")
@RestController
@RequestMapping("/bpm/handle-task")
@Validated
public class BpmHandleTaskController {

    @Resource
    private BpmHandleTaskService handleTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建审批流待办中心——任务")
    @PreAuthorize("@ss.hasPermission('bpm:handle-task:create')")
    public CommonResult<Long> createHandleTask(@Valid @RequestBody BpmHandleTaskCreateReqVO createReqVO) {
        return success(handleTaskService.createHandleTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审批流待办中心——任务")
    @PreAuthorize("@ss.hasPermission('bpm:handle-task:update')")
    public CommonResult<Boolean> updateHandleTask(@Valid @RequestBody BpmHandleTaskUpdateReqVO updateReqVO) {
        handleTaskService.updateHandleTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审批流待办中心——任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:handle-task:delete')")
    public CommonResult<Boolean> deleteHandleTask(@RequestParam("id") Long id) {
        handleTaskService.deleteHandleTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得审批流待办中心——任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:handle-task:query')")
    public CommonResult<BpmHandleTaskRespVO> getHandleTask(@RequestParam("id") Long id) {
        BpmHandleTaskDO handleTask = handleTaskService.getHandleTask(id);
        return success(BpmHandleTaskConvert.INSTANCE.convert(handleTask));
    }

    @GetMapping("/list")
    @Operation(summary = "获得审批流待办中心——任务列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('bpm:handle-task:query')")
    public CommonResult<List<BpmHandleTaskRespVO>> getHandleTaskList(@RequestParam("ids") Collection<Long> ids) {
        List<BpmHandleTaskDO> list = handleTaskService.getHandleTaskList(ids);
        return success(BpmHandleTaskConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得审批流待办中心——任务分页")
    @PreAuthorize("@ss.hasPermission('bpm:handle-task:query')")
    public CommonResult<PageResult<BpmHandleTaskRespVO>> getHandleTaskPage(@Valid BpmHandleTaskPageReqVO pageVO) {
        PageResult<BpmHandleTaskDO> pageResult = handleTaskService.getHandleTaskPage(pageVO);
        return success(BpmHandleTaskConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审批流待办中心——任务 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:handle-task:export')")
    @OperateLog(type = EXPORT)
    public void exportHandleTaskExcel(@Valid BpmHandleTaskExportReqVO exportReqVO,
                                      HttpServletResponse response) throws IOException {
        List<BpmHandleTaskDO> list = handleTaskService.getHandleTaskList(exportReqVO);
        // 导出 Excel
        List<BpmHandleTaskExcelVO> datas = BpmHandleTaskConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "审批流待办中心——任务.xls", "数据", BpmHandleTaskExcelVO.class, datas);
    }

}
